<template>
  <div class="mod-model">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="模型名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="processonDefinitionId"
        header-align="center"
        align="center"
        width="150"
        label="流程ID">
      </el-table-column>
      <el-table-column
        prop="key"
        header-align="center"
        align="center"
        label="模型标识">
      </el-table-column>
      <el-table-column
        prop="category"
        header-align="center"
        align="center"
        label="流程分类">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="模型名称">
      </el-table-column>
      <el-table-column
        prop="revision"
        header-align="center"
        align="center"
        label="版本号">
      </el-table-column>
      <el-table-column
        prop="deploymentTime"
        header-align="center"
        align="center"
        label="部署时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-dropdown :show-timeout="0" placement="bottom">
            <el-button type="primary">
              功能<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handlePic(scope.row, scope.index)">流程图</el-dropdown-item>
              <el-dropdown-item divided v-if="scope.row.suspend" @click.native="handleStatus(scope.row, 'active')">激活</el-dropdown-item>
              <el-dropdown-item divided v-if="!scope.row.suspend" @click.native="handleStatus(scope.row, 'suspend')">失效</el-dropdown-item>
              <el-dropdown-item divided v-if="!scope.row.suspend" @click.native="handleStartProcess(scope.row)">发起流程</el-dropdown-item>
              <el-dropdown-item divided @click.native="handleDel(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog title="流程图" :visible.sync="showPicDialog">
      <img :src="actPicUrl" width="100%">
    </el-dialog>
  </div>
</template>

<script>
import { getList, handleStatus, handleStartProcess, handleDel } from '../../api/lcgl/process'
import { getToken } from '../../utils/auth'
import { lcglModulePrefix } from '../../config'
export default {
  data() {
    return {
      dataForm: {
        current: 1,
        size: 10,
        name: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showPicDialog: false,
      backendUrl: `${process.env.BASE_API}${lcglModulePrefix}`,
      actPicUrl: ''
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      getList(this.dataForm).then(res => {
        if (res && res.code === 0) {
          this.dataList = res.data.records
          this.totalPage = res.data.pages
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      }).catch(function(err) {
        console.log(err)
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    handlePic(row, index) {
      this.actPicUrl = this.backendUrl +
        `/process/resource/` +
        row.deploymentId +
        '/' +
        row.processonDefinitionId +
        '/image?access_token=' +
        getToken()
      this.showPicDialog = true
    },
    handleStatus(row, type) {
      var _this = this
      this.$confirm(
        '是否确认操作ID为"' + row.processonDefinitionId + '"的流程?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(function() {
        return handleStatus(row, type)
      }).then(({ data }) => {
        _this.getDataList()
        _this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      }).catch(function(err) {
        console.log(err)
      })
    },
    handleStartProcess(row) {
      var _this = this
      this.$confirm(
        '是否确认发起部署ID为"' + row.deploymentId + '"的流程?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(function() {
        return handleStartProcess(row)
      }).then(({ data }) => {
        _this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      }).catch(function(err) {
        console.log(err)
      })
    },
    handleDel(row) {
      var _this = this
      this.$confirm(
        '是否确认删除部署ID为"' + row.deploymentId + '"的流程?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(function() {
        return handleDel(row)
      }).then(({ data }) => {
        _this.getDataList()
        _this.$message({
          showClose: true,
          message: '操作成功',
          type: 'success'
        })
      }).catch(function(err) {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>

.el-pagination {
  text-align: center;
}
</style>
